<?php

/**
 * FootballFactory, Copyright Robin Loop & Pascal Wirz, 2014
 * ---
 * Repraesentiert ein Spieltyo
 * Methoden, um Spieltypen aus der DB abzurufen, editieren und loeschen
 *
 * @author Lopo
 */
class Gametype {
    private $id = null;
    private $name = null;
    private $home = null;


	public function __construct($data = array()) {
        if (isset($data['gametype_id'])) $this->id = (int) $data['gametype_id'];
        if (isset($data['gametype_name'])) $this->name = $data['gametype_name'];
        if (isset($data['gametype_home'])) $this->home = (bool) $data['gametype_home'];
    }
	
	public static function getGametypeById($id) {
        $db = new Database();

        $sql = "SELECT * FROM ff_gametypes WHERE gametype_id = :id";

        $stmt = $db->prepare($sql);
        $stmt->bindValue(":id", $id, PDO::PARAM_INT);
        $stmt->execute();
        $row = $stmt->fetch();
        
        $db = null;

        if ($row) return new Gametype($row);
    }
	
	public static function getGametypeList() {
        $db = new Database();

        $sql = "SELECT * FROM ff_gametypes";

        $stmt = $db->prepare( $sql );
        $stmt->execute();
        
        $list = array();
        while ($row = $stmt->fetch()) {
            $gametype = new Gametype($row);
            $list[] = $gametype;
        }

        $sql = "SELECT FOUND_ROWS() AS totalRows";
        $totalRows = $db->query($sql)->fetch();
        
        $db = null;

        return (array("gametypeList" => $list, "totalRows" => $totalRows[0]));
    }
    
    public static function getGameTypeByGameId($gameId) {
        $db = new Database();

        $sql = "SELECT * FROM ff_gametypes gt, ff_games g WHERE g.game_type = gt.gametype_id AND g.game_id = :gameId";

        $stmt = $db->prepare($sql);
        $stmt->bindValue(":gameId", $gameId, PDO::PARAM_INT);
        $stmt->execute();
        $row = $stmt->fetch();
        
        $db = null;

        if ($row) return new Gametype($row);
        
    }
	
	public function insert() {
        if ($this->id != 0) {
            trigger_error("Gametype::insert(): Es wird versucht ein neuer Spietyp anzulegen, obwohl bereits die ID gesetzt wurde (ID: $this->id).", E_USER_ERROR);
        }

        $db = new Database();

        $sql = "INSERT INTO ff_gametypes (gametype_name, gametype_home) VALUES (:name, :home)";
		
		$stmt = $db->prepare($sql);
        $stmt->bindValue(":name", $this->name, PDO::PARAM_STR);
        $stmt->bindValue(":home", $this->home, PDO::PARAM_INT);
        $stmt->execute();
        
        $this->id = $db->lastInsertId();
        
        $db = null;
    }
	
	public function update() {
        if (is_null($this->id)) {
            trigger_error("Gametype::update(): Es wird versucht ein Spieltyp zu speichern, der keine ID hat - was nicht geht beim Update.", E_USER_ERROR);
        }

        $db = new Database();

        $sql = "UPDATE ff_gametypes SET gametype_name=:name, gametype_home=:home WHERE gametype_id=:id";
		
        $stmt = $db->prepare($sql);
		$stmt->bindValue(":name", $this->name, PDO::PARAM_STR);
        $stmt->bindValue(":home", $this->home, PDO::PARAM_INT);
        $stmt->bindValue(":id", $this->id, PDO::PARAM_INT);
        $stmt->execute();
        
        $db = null;
    }
	
	public function delete() {
        if (is_null($this->id)) {
            trigger_error("Gametype::delete(): Es wird versucht ein Spieltyp zu loeschen, der keine ID hat - was nicht geht beim Loeschen.", E_USER_ERROR);
        }

        $db = new Database();

        $sql = "DELETE FROM ff_gametypes WHERE gametype_id = :id LIMIT 1";
        
        $stmt = $db->prepare($sql);
        $stmt->bindValue(":id", $this->id, PDO::PARAM_INT);
        $stmt->execute();
        
        $db = null;
    }
    public function getId() {
        return $this->id;
    }

    public function getName() {
        return $this->name;
    }

    public function getHome() {
        return $this->home;
    }


}
?>
